/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
 
package xml.valueobject;

import java.io.File;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import xml.utils.DBUtils;

/**
 *
 * @author mrnghiep
 */
public class InfoSAXProcessor {

    InformationList informations = new InformationList();

    public InfoSAXProcessor() {
        Connection con = null;
        CallableStatement call = null;
        ResultSet rs = null;
        try {
            con = DBUtils.makeConnection();
            call = con.prepareCall("{call getListInfo()}");
            rs = call.executeQuery();

            int id = 0;
            String fullName = "";
            String address = "";
            String phone = "";
            String email = "";
            String info = "";
            String type = "";
            String location = "";
            String owner = "";
            String imageUrl = "";
            String description = "";
            String date = "";
//            DateFormat format = new SimpleDateFormat("yyyy/mm/dd HH:mm:ss");
//            Date fDate = null;
//            GregorianCalendar gregory = new GregorianCalendar();
//            XMLGregorianCalendar postDate = null;

            String category = "";

            while (rs.next()) {
                id = rs.getInt("id");
                fullName = rs.getString("fullName");
                address = rs.getString("address");
                phone = rs.getString("phone");
                email = rs.getString("email");
                info = rs.getString("info");
                type = rs.getString("type");
                location = rs.getString("location");
                owner = rs.getString("owner");
                imageUrl = rs.getString("imageUrl");
                description = rs.getString("description");

                date = rs.getString("date");
//                fDate = format.parse(date);
//                gregory.setTime(fDate);
//                postDate = DatatypeFactory.newInstance().newXMLGregorianCalendar(gregory);
                System.out.println(fullName + " " + date);
                category = rs.getString("category");

                Information information = new Information(id, fullName, address, phone, email, info, type, location, owner, imageUrl, description, date, category);
                informations.getListInfo().add(information);
            }
        } catch (SQLException e) {
            Logger.getLogger(InfoSAXProcessor.class.getName()).log(Level.SEVERE, null, e);
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (call != null) {
                    call.close();
                }
                if (con != null) {
                    con.close();
                }
            } catch (SQLException e) {
                Logger.getLogger(InfoSAXProcessor.class.getName()).log(Level.SEVERE, null, e);
            }
        }
    }

    public void marshallXML(String fileName) {
        try {
            JAXBContext jaxb = JAXBContext.newInstance(InformationList.class);
            Marshaller mar = jaxb.createMarshaller();
            mar.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            mar.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
            mar.marshal(informations, new File(fileName));
        } catch (Exception e) {
            Logger.getLogger(InfoSAXProcessor.class.getName()).log(Level.SEVERE, null, e);
        }
    }
}
